import * as vscode from "vscode"

let targetButton: vscode.StatusBarItem
let buttons: Array<vscode.StatusBarItem>

export default {
	initStatusBar(context: vscode.ExtensionContext) {
		const items = [
			["$(inbox) Please open a project.", "easy-aos.setTarget"],
			["$(check) Build", "easy-aos.build"],
			["$(flame) Flash", "easy-aos.flash"],
			["$(terminal) Monitor", "easy-aos.monitor"],
			["$(trashcan) Clean", "easy-aos.clean"]
		]
		buttons = []
		items.reverse().forEach((item, index) => {
			this.addStatusBar(context, item, index + 200)
		})
	},

	configTarget(name: string): void {
		targetButton.text = `$(inbox) ${name}`
	},

	hideButtion(): void {
		buttons.forEach((button) => {
			button.hide()
		})
	},

	showButton(): void {
		buttons.forEach((button) => {
			button.show()
		})
	},

	addStatusBar(context: vscode.ExtensionContext, statusItem: string[], index: number) {
		const [text, command] = statusItem
		const sbItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, index)
		sbItem.text = text
		sbItem.command = command
		sbItem.show()
		if (command === "easy-aos.setTarget") {
			targetButton = sbItem
		} else {
			buttons.push(sbItem)
		}
		context.subscriptions.push(sbItem)
		return sbItem
	}
}
